package com.springboot.controller;

import com.springboot.dto.PatientChatIndex;
import com.springboot.model.ChatRecord;
import com.springboot.model.UserLoginForm;
import com.springboot.service.FileUploadService;
import com.springboot.service.PatientChatService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Controller
public class PatientChatController {
    @Autowired
    PatientChatService patientChatService;

    @Autowired
    UserLoginService userLoginService;

    @Autowired
    FileUploadService fileUploadService;

    /*
     * @ description:患者展示聊天列表
     * @ param      :[ArrayList<PatientChatIndex>] 患者的手机号
     * @ return     :java.lang.String
     * @ date       :2021/1/1

    */
    @CrossOrigin
    @RequestMapping(value = "/showpatientchatindex",method = RequestMethod.GET)
    @ResponseBody
    ArrayList<PatientChatIndex> showPatientChatIndex(@RequestParam("unitphone") String patientTel)
    {
        System.out.println("用户展示聊天列表:手机号:"+patientTel);
        UserLoginForm userLoginForm = new UserLoginForm();
        userLoginForm = userLoginService.findUserByTel(patientTel);
        ArrayList<PatientChatIndex> patientChatIndices = new ArrayList<>();
        patientChatIndices = patientChatService.showChatIndex(userLoginForm.getUserId());
        return patientChatIndices;
    }

    /**
     * @ description:用户删除聊天记录
     * @ param      :[patientTel, doctorId]
     * @ return     :int
     * @ date       :2021/1/8

    */
    @CrossOrigin
    @RequestMapping(value = "/deletepatientonechatindex",method = RequestMethod.GET)
    @ResponseBody
    public int deletePatientOneChatIndex(@RequestParam("tel") String patientTel ,
                                         @RequestParam("doctorId") Integer doctorId)
    {
        System.out.println("用户删除一条聊天列表:手机号:"+patientTel);
        UserLoginForm userLoginForm = new UserLoginForm();
        userLoginForm = userLoginService.findUserByTel(patientTel);
        return patientChatService.deleteChatInfo(userLoginForm.getUserId(),doctorId);
    }

    /**
     * @ description:患者展示和某医生的聊天
     * @ param      :[patientTel, doctorId]
     * @ return     :java.util.ArrayList<com.springboot.dto.PatientChatIndex>
     * @ date       :2021/1/1

    */
    @CrossOrigin
    @RequestMapping(value = "/showpatientchatinfo",method = RequestMethod.GET)
    @ResponseBody
    ArrayList<PatientChatIndex> showPatientChatInfo(@RequestParam("unitphone") String patientTel,
                                                    @RequestParam("doctorId") Integer doctorId)
    {
        UserLoginForm userLoginForm = userLoginService.findUserByTel(patientTel);
        Integer patientId = userLoginForm.getUserId();
        ArrayList<PatientChatIndex> patientChatIndices = new ArrayList<>();
        patientChatIndices = patientChatService.showPatientChatInfoByTwoId(patientId,doctorId);

        return patientChatIndices;
    }

    /**
     * @ description:添加一条聊天信息，不含照片
     * @ param      :[patientTel, doctorId, content]
     * @ return     :java.lang.String
     * @ date       :2021/1/2

    */
    @CrossOrigin
    @RequestMapping(value = "/patientaddchatinfo",method = RequestMethod.GET)
    @ResponseBody
    String patientAddChatInfo(@RequestParam("unitphone") String patientTel, @RequestParam("doctorId") Integer doctorId,
                              @RequestParam("content") String content)
    {
        UserLoginForm userLoginForm = userLoginService.findUserByTel(patientTel);
        Integer patientId = userLoginForm.getUserId();
        ChatRecord chatRecord = new ChatRecord();
        chatRecord.setChatContent(content);
        chatRecord.setSenderId(patientId);
        chatRecord.setReceiverId(doctorId);
        chatRecord.setFlag2(0);
        int a =  patientChatService.insertChatInfoByChatRecord(chatRecord);
        return a+"";
    }

    /*
     * @ description:聊天时，患者发送照片
     * @ param      :[patientTel, doctorId, file]
     * @ return     :java.lang.String
     * @ date       :2021/1/2

    */
    @CrossOrigin
    @RequestMapping(value = "/patientaddchatphoto")
    @ResponseBody
    String patientAddChatPhoto(@RequestParam("unitphone") String patientTel, @RequestParam("doctorId") Integer doctorId,
                               @RequestParam("filePhoto") MultipartFile file)
    {
        UserLoginForm userLoginForm = userLoginService.findUserByTel(patientTel);
        Integer patientId = userLoginForm.getUserId();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String path = simpleDateFormat.format(date);
        String filepath = fileUploadService.UploadPicture(file,path);
        ChatRecord chatRecord = new ChatRecord();
        chatRecord.setSenderId(patientId);
        chatRecord.setReceiverId(doctorId);
        chatRecord.setPhotoLink(filepath);
        chatRecord.setFlag2(1);
        int a =  patientChatService.insertChatInfoByChatRecord(chatRecord);
        return a+"";
    }

}
